package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWmUserClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private  ApUserRealnameMapper apUserRealnameMapper;

    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private IWmUserClient iWmUserClient;
    @Override
    public ResponseResult list(AuthDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();


        Integer page = dto.getPage();
        Integer size = dto.getSize();

        // 3.构建分页条件
        Page<ApUserRealname> pageInfo = new Page<>(page, size);

        // 4.构建查询条件
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus());

        // 5.执行查询
        Page<ApUserRealname> pageResult = apUserRealnameMapper.selectPage(pageInfo, wrapper);

        // 6.组装查询结果
        Long total = pageResult.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page, size, total.intValue());
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult authFail(AuthDto dto, Short status) {
        //审核不通过
        if (dto == null || dto.getId() == null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        return null;

    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ResponseResult auth(AuthDto dto, Integer type) {

        if (dto == null) {
        return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //获得id
        Integer id = dto.getId();
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(id);
        if (apUserRealname == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);

        }

        //更新状态
        apUserRealname.setId(id);
        //状态
        apUserRealname.setStatus((short) (type == 1 ? 9 :2));
        apUserRealname.setReason(dto.getMsg());
        apUserRealnameMapper.updateById(apUserRealname);

        //通过
        if (type == 1) {
            String name = apUserRealname.getName();
            Map<String ,String >map = new HashMap<>();
            map.put("name",name);
            ResponseResult responseResult = iWmUserClient.addUser(map);
            if (responseResult == null || responseResult.getCode() != 200){
                throw new RuntimeException("创建失败");
            }
        }
        //更新状态
        ApUser apUser = new ApUser();
        apUser.setId(apUserRealname.getId());
        apUser.setFlag((short) 1);
        int update = apUserMapper.updateById(apUser);
        if (update < 1 ){
            throw  new RuntimeException("状态更新失败");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
